<template>
  <base-breadcrumb :breadcrumb="!isAudit">
    <template>
      <a-spin :spinning="loading">
        <a-steps direction="vertical" :current="current" @change.self="onChangeStep">
          <a-step title="分包签证指令单">
            <div v-if="current == 0" slot="description">
              <instructionSheet ref="instructionSheet" :currentStep="currentStep" :instructionSheetData="form.visaSerVO" :editModel="editModel"></instructionSheet>
            </div>
          </a-step>
          <a-step title="分包签证事实确认单" :disabled="currentStep < 1">
            <div v-if="current == 1 && form.factBusinessDTO" slot="description">
              <confirmationFact :currentStep="currentStep" :confirmationFactData="form.factBusinessDTO" :editModel="editModel"></confirmationFact>
            </div>
          </a-step>
          <a-step title="分包签证经济签证单" :disabled="currentStep < 2">
            <div v-if="current == 2 && form.economicVisaBusinessDTO" slot="description">
              <visaSingle :currentStep="currentStep" :visaSingleData="form.economicVisaBusinessDTO" :editModel="editModel"></visaSingle>
            </div>
          </a-step>
          <a-step title="资料汇总" :disabled="currentStep < 3">
            <div v-if="current == 3 && form.fileSerDTO" slot="description">
              <dataSummary :currentStep="currentStep" :fileSerDTOData="form.fileSerDTO" :editModel="editModel"></dataSummary>
            </div>
          </a-step>
        </a-steps>
        <!-- v-if="current != currentStep" -->
        <div v-if="!isConsultation" class="btn_box page-btn-right-top">
          <print-button v-if="form.economicVisaBusinessDTO && form.economicVisaBusinessDTO.auditStatus == 'PAST'&&editModel==0"  button-text="打印" :before-print-fn="managementPrint" />
          <a-button class="ml12" @click="handleActiveClose">关闭</a-button>
        </div>
      </a-spin>
    </template>
  </base-breadcrumb>
</template>
<script>
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 分公司
import SelectSubcontractor from '@/components/Select/SelectSubcontractor' // 分公司
import instructionSheet from './components/instructionSheet'
import confirmationFact from './components/confirmationFact'
import dataSummary from './components/dataSummary'
import visaSingle from './components/visaSingle'
import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置
import { getDetail, managementPrint } from '@/api/businessAffairs/subcontractVisa'
import printButton from '@/components/BasePrintButton' // 打印
export default {
  name: 'a' + Date.now(),
  extends: new BaseEditPage(),
  components: {
    SelectSubCompany,
    SelectProject,
    SelectSubcontractor,
    instructionSheet,
    confirmationFact,
    dataSummary,
    visaSingle,
    printButton
  },
  data() {
    return {
      form: {
        branchComId: '',
        projectId: '',
        name: '',
        visaSerVO: { serDTOS: [] },
        fileSerDTO: { fineDTOS: [] },
        factBusinessDTO: { businessAgentId: '', projectDeptId: '' },
        contractNameId: ''
      },
      loading: false,
      current: -1,
      currentStep: -1
    }
  },
  props: {
    // 是否作为审批详情
    isAudit: {
      type: Boolean,
      default: false
    },
    // 是否作为意见征询
    isConsultation: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    isAudit: {
      immediate: true,
      handler(val) {
        if (val) {
          this.$nextTick(() => {
            this.editModel = 0
          })
        }
      }
    }
  },
  created() {},
  mounted() {
    this.$route.meta.title = ['分包签证查看', '分包签证编辑', '分包签证新增'][this.editModel]
    // 不为添加时请求详情
    if (this.editModel != 2) {
      this.getDetailReq()
    } else {
      this.form.visaSerVO.projectId = this.$userInfo().currentPost.projectId;
      this.$nextTick(() => {
        // 默认映射联系人为登陆人
        this.current = 0
        this.currentStep = 0
        this.init = true
      })
    }
  },
  methods: {
    // 打印
    managementPrint() {
      let that = this
      return new Promise((resolve, reject) => {
        let data = { id: this.id }
        managementPrint(data)
          .then((res) => {
            resolve(res.data.content)
          })
          .catch((e) => {
            that.$message.error(e)
          })
          .finally(() => {
            resolve()
          })
      })
    },
    onChangeStep(current) {
      console.log(current)
      this.current = current
    },
    getDetailReq() {
      this.loading = true
      getDetail({ id: this.id }).then((res) => {
        //数据转换
        res.data.visaSerVO.isOtherDeduction = res.data.visaSerVO.isOtherDeduction ? 1 : 0
        //后续步骤要显示步骤一的字段后端没反
        if (res.data.factBusinessDTO) {
          res.data.factBusinessDTO.branchComId = res.data.visaSerVO.branchComId
          res.data.factBusinessDTO.projectId = res.data.visaSerVO.projectId
          res.data.factBusinessDTO.subContractUnitId = res.data.visaSerVO.subContractUnitName
          res.data.factBusinessDTO.name2 = res.data.visaSerVO.name
          res.data.factBusinessDTO.constructionSite = res.data.visaSerVO.constructionSite
          res.data.factBusinessDTO.codeStep1 = res.data.visaSerVO.code
        }
        if (res.data.economicVisaBusinessDTO) {
          res.data.economicVisaBusinessDTO.branchComId = res.data.visaSerVO.branchComId
          res.data.economicVisaBusinessDTO.projectId = res.data.visaSerVO.projectId
          res.data.economicVisaBusinessDTO.subContractUnitId = res.data.visaSerVO.subContractUnitName
          res.data.economicVisaBusinessDTO.name3 = res.data.visaSerVO.name
          res.data.economicVisaBusinessDTO.constructionSite = res.data.visaSerVO.constructionSite
          res.data.economicVisaBusinessDTO.matters = res.data.visaSerVO.matters
        }
        if (res.data.fileSerDTO) {
          res.data.fileSerDTO.isOtherDeduction = res.data.visaSerVO.isOtherDeduction
          res.data.fileSerDTO.initialVisaEnd = res.data.economicVisaBusinessDTO.initialVisaEnd
          res.data.fileSerDTO.initialVisaTaxEnd = res.data.economicVisaBusinessDTO.initialVisaTaxEnd
          res.data.fileSerDTO.initialVisaTotalEnd = res.data.economicVisaBusinessDTO.initialVisaTotalEnd
          res.data.fileSerDTO.projectId = res.data.factBusinessDTO.projectId
        }
        if (res.data.visaSerVO) {
          this.current = 0
          this.currentStep = 0
        }
        if (res.data.factBusinessDTO) {
          this.current = 1
          this.currentStep = 1
        }
        if (res.data.economicVisaBusinessDTO) {
          this.current = 2
          this.currentStep = 2
        }
        if (res.data.fileSerDTO) {
          this.current = 3
          this.currentStep = 3
        }
        if (this.editModel == 0) {
          this.current = 0
        }

        this.loading = false

        this.form = res.data

        console.log(this.form)
      })
    }
  }
}
</script>
